export async function getCookie(name) {
  return new Promise((resolve, reject) => {
    try {
      const value = `; ${document.cookie}`;
      const parts = value.split(`; ${name}=`);
      if (parts.length === 2) {
        resolve(parts.pop().split(';').shift());
      } else {
        resolve(null);  // 如果指定的 Cookie 不存在，返回 null
      }
    }catch (error) {
      resolve(null);
    }
  })
  // const value = `; ${document.cookie}`;
  // const parts = value.split(`; ${name}=`);
  // if (parts.length === 2) {
  //   return parts.pop().split(';').shift();
  // }
  // return null;  // 如果指定的 Cookie 不存在，返回 null
}
export async function setCookie(name, value, days) {
 return new Promise((resolve, reject) => {
  try {
    let maxAge = '';
  if (days) {
    maxAge = `; max-age=${days * 24 * 60 * 60}`; // 将天数转换为秒
  }
  document.cookie = `${name}=${value || ''}${maxAge}; path=/; domain=localhost`; // 设置 cookie，路径为根路径
  resolve(true)
  } catch (error) {
    resolve(true)
  }
  });
}
export function deleteCookie(name, path = '/', domain = '') {
  // 设置过期时间为过去的日期，删除 cookie
  const cookieString = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=${path};`;

  // 如果设置了 domain，则添加 domain
  if (domain) {
    document.cookie = cookieString + `domain=${domain};`;
  } else {
    document.cookie = cookieString;
  }
}